	<?php
	/**
	* @version		$Id: dynl_html.php 1 2007-01-11 22:03:46Z friesengeist $
	* @package		Dynlist
	* @copyright	Copyright (C) 2005 - 2007 Dynlist. All rights reserved.
	* @license		GNU/GPL, see LICENSE.php
	* Dynlist is free software. This version may have been modified pursuant
	* to the GNU General Public License, and as distributed it includes or
	* is derivative of works licensed under the GNU General Public License or
	* other free or open source software licenses.
	* See COPYRIGHT.php for copyright notices and details.
	*/
	/**
	* Dynlist HTML class
	*
	* Provide many supporting of HTML render pages.
	*
	* @package		Dynlist
	* @final
	*/
	class dynl_html
	{
		/**
		 * Dynlist name
		 * 
		 * @var char
		 * @access protected
		*/
		var $_dynlname = null;
		
		
		/**
		 * Contents all data needed to render the page.
		 *
		 * @var array
		 * @access protected
		 */
		var $_content = null;
		
		/**
		* Class constructor
		*
		* @access protected
		*/
		function dynl_html()
		{

		}

		/**
		 * Set Dynlist name.
		 *
		 * @access protected
		 * @param char dynlname
		*/
		function set_dynlname($dynlname)
		{
			$this->_dynlname = $dynlname;
		}
		
		/**
		 * Set HTML content.
		 *
		 * @access protected
		 * @param array content
		*/
		function set($content)
		{
			$this->_content = $content;
		}
			
	}

	/**
	* Dynlist catalog html class
	*
	* Provide many supporting of HTML render page for Dynlist Catalog information.
	*
	* @package		Dynlist
	* @final
	*/
	class html_dynlcat extends dynl_html
	{
		/**
		* Class constructor
		*
		* @access protected
		*/
		function html_dynlcat()
		{
			parent::dynl_html();
		}

		/**
		* Render HTML page for Dynlist Catalog information.
		*
		* @access protected
		*/
		function render_page()
		{
			// Link to new strucuture
			$output .= '<a href="?xcode=!snew">New</a>';
			$output .= '<ol>'."\n";
			
			// Get number of records.
			$size = (count($this->_content) - 1);
			
			// Loop at all records to print.
			// This loop starts at 1, because first is the columns labels.
		for ($i = 1; $i <= $size; $i++){
			$record = $this->_content[$i];
			$output .= '<li>'.$record[1].	
			           '(<a href="?xcode=data!'.$record[0].'!show_data">Show</a>'.
	  				   '/<a href="?xcode=structure!'.$record[0].'!edit">Edit</a>)</li>'."\n";
		}
			
	  	$output .= '</ol>'."\n";						
		
	  	// Print output.
	  	echo $output;
	}
}

/**
* Dynlist data html class
*
* Provide many supporting of HTML render page for Dynlist Data information.
*
* @package		Dynlist
* @final
*/
class html_data extends dynl_html
{
	/**
	 * Contents all table headers needed to render the page.
	 *
	 * @var array
	 * @access protected
	 */
	var $_header = null;

        /**
         * Key fields of this dynlist.
         *
         * @var array
         * @access protected
         */
        var $_keyfields = null;

	/**
	 * Contents all commands needed to render the page.
	 *
	 * @var array
	 * @access protected
	 */
	var $_commands = null;
	
	/**
	* Class constructor
	*
	* @access protected
	*/
	function html_data()
	{
		parent::dynl_html();
	}

        /**
        * Set keys fields.
        *
        * @access protected
        */
        function set_keyfields($keyfields)
	{
                $this->_keyfields = $keyfields;
        }

	/**
	 * Set Header texts.
	 *
	 * @access protected
	*/
	function set_header($header)
	{
		$this->_header = $header;	
	}

	/**
	 * Render HTML page for Dynlist Data information.
	 *
	 * @access protected
	*/
	function render_page()
	{
		$output = '<table width="100%" border="2" cellspacing="1" cellpadding="0" align="center">';

		// Render commands.
		$output .= $this->render_commands();
		
		// Render header.
		$output .= $this->render_data_header();
		
		// Render data.
		$output .= $this->render_data();
		
		$output .= '</table>';
		
		//print_r($this->_keyfields);
	  	// Print output.
	  	echo $output;	
		
	}
	
	/**
	* Render commands.
	*
	* @access protected
	*/
	function render_commands()
	{
		$output .= '<a href="?xcode=data!'.$this->_dynlname.'!new_record">New Record</a>'."\n";
		return $output;
	}

	/**
	* Render data headers.
	*
	* @access protected
	*/
	function render_data_header()
	{
		//print_r($this->_header);
		// Declare table.
		//$output .= '<table width="100%" border="2" cellspacing="1" cellpadding="0" align="center">';
		
		// First row.
		$output .= '<tr>';

		for ($i = 0; $i <= count($this->_header); $i++){
			$h_header = $this->_header[$i];
			// print short text.
			$output .= '<td><b>'.$h_header[0].'</b></td>';
		}
		
		$output .= '<td><b>Edit</b></td>';
		$output .= '<td><b>Delete</b></td>';

		// End of first row;
		$output .= '</tr>';
		return $output;

	}
	
	/**
	* Render data records.
	*
	* @access protected
	*/
	function render_data()
	{
		//print_r($this->_content[1]);
		for ($i=1; $i <= (count($this->_content) - 1); $i++){
			$output .= '<tr>';
			$h_content = $this->_content[$i];
			//print_r($this->_content[1]);
			for ($j = 0; $j <= (count($h_content) - 2); $j++){
				
				if ($h_content[$j] == '') {
					$output .= '<td> </td>';
				} else {
					$output .= '<td>'.$h_content[$j].'</td>';
				}
			}
			
			// Commands. class object method.
			$output .= '<td><a href="?xcode=data!'.$this->_dynlname.'!edit_record!'.$h_content[count($h_content)-1].'">Edit</a></td>';
			$output .= '<td><a href="?xcode=data!'.$this->_dynlname.'!delete_record!'.$h_content[count($h_content)-1].'">Delete</a></td>';
			
			$output .= '</tr>';
		}	
		
		return $output;
	}
}

/**
* Dynlist layout html class
*
* Provide many supporting of HTML render page for Dynlist Layout information.
*
* @package		Dynlist
* @final
*/
class html_layout extends dynl_html{
	/**
	* Class constructor
	*
	* @access protected
	*/
	function html_layout()
	{
		parent::dynl_html();
	}

	/**
	* Render HTML page for Dynlist Layout information.
	*
	* @access public
	*/
	function render_page()
	{

	}	
}

/**
* Dynlist strucuture html class
*
* Provide many supporting of HTML render page for Dynlist Structure information.
*
* @package		Dynlist
* @final
*/
class html_structure extends dynl_html{
	/**
	* Class constructor
	*
	* @access protected
	*/
	function html_structure()
	{
		parent::dynl_html();
	}

	/**
	* Render HTML page for Dynlist Structure information.
	*
	* @access protected
	*/
	function render_page()
	{
		
	}
}

/**
* Dynlist record html class
*
* Provide many supporting of HTML render page for Dynlist record maintenance.
*
* @package              Dynlist
* @final
*/
class html_datarec extends dynl_html{
        
	/**
	 * Field text labels
	 *
	 * @var array
	 * @access protected
	 */
	var $_labels = null;
	
	/**
	 * Key Fields
	 *
	 * @var array
	 * @access protected
	 */
	var $_keyfields = null; 
	
	/**
	 * Class constructor
	 *
	 * @access protected
	 */
	function html_datarec()
	{
		parent::dynl_html();
	}

	/**
	 * Set field text labels.
	 *
	 * @access protected
	 */
	function set_labels($labels)
	{
		$this->_labels = $labels;
	} 


        /**
	* Set key fields.
	*
	* @access protected
	*/
	function set_keyfields($keyfields)
	{
		$this->_keyfields = $keyfields;
	}

	/**
	 * Render HTML page for Dynlist data record information.
	 *
	 * @access protected
	 */
	function render_page($fcode)
	{
		//print_r($this->_labels);
		//print_r($this->_keyfields);
		
		//$output .= '<a href="javascript:history.go(-1)">Voltar</a>';

		// Start form.
		$output .= '<form action="./posts/post_edit_record.php" method="post">';

		// Set hidden field - Dynlist name.
		// This information is used to assembly link command to save record.
		$output .= '<input type="hidden" id="hidden_dynlname" name="hidden_dynlname" value="'.$this->_dynlname.'"';
		
		// Start table. This table is used to align labes and fields.
		$output .= '<table width="100%" border="0" cellspacing="1" cellpadding="0" align="center">';

		// Render fields.
		$output .= $this->render_fields($fcode);
						
		// Show buttons commands.
		$output .= $this->render_commands();
		                
		// Close table.
		$output .= '</table>';
						
		// Close form.
		$output .= '</form>';

		// show output.
		echo $output;
		
	}

        /**
	* Render HTML page with fields to edit.
	*
	* @access protected
	*/
 	function render_fields($fcode)
	{
                //echo 'data-->';
                //print_r($this->_content[0]);
                //echo '<--data';

		// Get field names from _data.
		$fieldnames = $this->_content[0];
		
		// Get field values from _data.
		$fieldvalues = $this->_content[1];
		
		// Start counter.
		$i = 0;
		
		// Define format for key fields.
		foreach ($fieldnames as $h_fieldname)
		{
			// If this field is key, do not allow to edit.
			if (in_array($h_fieldname, $this->_keyfields))
			{
				if ($fcode == 'edit_record')
				{
					$disabled = 'disabled';
				}
				$key_mark_start = '<b>';
				$key_mark_end = '</b>';
			} else {
				$disabled = '';
				$key_mark_start = '';
				$key_mark_end = '';
			}
			 
			// Get label.
			$label_rec = $this->_labels[$i];
			$label = $label_rec[0];

			// Get actual value.
			$value = $fieldvalues[$i];

			// Render field.
			$output .= '<tr><td><label for="'.$h_fieldname.'">'.$key_mark_start.$label.$key_mark_e.'</label></td>'."\n";
			
			// If field is a key:
			if ($disabled <> '')
			{
				$output .= '<td><input '.$disabled.' type="text" id="key_'.$h_fieldname.'" name="'.$h_fieldname.'" value="'.$value.'" size="50">'."\n";
				
				// For disable fields, it's necessary include hidden field to read at POST method.
				$output .= '<input type="hidden" id="'.$h_fieldname.'" name="'.$h_fieldname.'" value="'.$value.'" size="50"></td></tr>'."\n";
			} else {
				// Otherwise, it's not necessary include hidden field.
				$output .= '<td><input '.$disabled.' type="text" id="'.$h_fieldname.'" name="'.$h_fieldname.'" value="'.$value.'" size="50"></td></tr>'."\n";
			}
			// Increment counter.
			$i++;
		}
		
		// Set hidden field - fields names;
		// This information is used to get dynamiclly values from POST method.
		$output .= '<input type="hidden" id="hidden_fieldlist" name="hidden_fieldlist" value="'.$this->get_fieldlist().'"';	
		return $output;
	}

	/**
	 * Render HTML page with commands.
	 * 
	 * @access protected
	 */
	function render_commands()
	{
		$output .= '<td>';
		$output .= '<input type="submit" name="action" value="Save"/>';
		$output .= '<input type="submit" name="action" value="Cancel"/>';
		$output .= '<input type="submit" name="action" value="New Record"/>';
		$output .= '</td>';
		/*
		$output .= '<td>';
		$output .= '<input type="submit" name="action" value="Cancel"/>';
		$output .= '</td>';
		
		$output .= '<td>';
		$output .= '<input type="submit" name="action" value="New Record"/>';
		$output .= '</td>';
		*/
		return $output;
	} 

	/**
	 * Return a list of fields names separed by ';'
	 *
	 * @access protected
	 */
	function get_fieldlist()
	{
		// Get field names from _data.
		$list = $this->_content[0];
		
		$i = 0;
		foreach ($list as $value)
		{
			$fieldlist .= $value;
			if ($i <= count($list))
			{
				$fieldlist .= ';';
			}
			$i++;
		}
		
		return $fieldlist;
	}
	
}
?>
